package com.gump.essay.demo.design.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 动态代理
 *
 * @auther zzc
 * @date 2020/12/9 15:19
 */
public class DynamicProxy implements InvocationHandler {

    //代理的目标对象
    private Object object;

    public DynamicProxy(Object object) {
        this.object = object;
    }

    //生成动态代理对象
    public Object proxy(){
        Class<?> clazz = object.getClass();
        //生成代理对象
        return Proxy.newProxyInstance(clazz.getClassLoader(),clazz.getInterfaces(),this);
    }

    /**
     *
     * @param proxy  动态生成的代理对象
     * @param method 代理方法
     * @param args   代理方法的方法参数
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        beforeMethod(object);
        //反射执行代理对象的目标方法
        Object result = method.invoke(object, args);
        afterMethod(object);
        return result;
    }

    private void beforeMethod(Object object) {
        if(object instanceof IceCreamService){
            System.out.println("准备冰淇淋材料");
        }else{
            throw new RuntimeException("暂不支持代理"+object.getClass()+"类型");
        }
    }

    private void afterMethod(Object object) {
        if(object instanceof IceCreamService){
            System.out.println("保鲜冰淇淋");
        }else{
            throw new RuntimeException("暂不支持代理"+object.getClass()+"类型");
        }
    }


}
